package com.lmpop.trade.taskScheduler;

import com.lmpop.frame.util.RandomID;
import com.lmpop.trade.tb_emailrecord.Tb_emailrecordInfo;
import com.lmpop.trade.ti_emaillist.Ti_emaillistInfo;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Hashtable;
import java.util.List;
import java.util.Map;

public class TaskScheduler
{
  Tb_emailrecordInfo emailRecordInfo = new Tb_emailrecordInfo();
  RandomID randomID = new RandomID();

  public void runTaskScheduler()
  {
    List taskList = null;

    Ti_emaillistInfo emailListInfo = new Ti_emaillistInfo();

    Map taskConditionMap = new Hashtable();

    taskConditionMap.put("Current", getCurrentDate());

    taskList = emailListInfo.taskScheduler(taskConditionMap);
    String email = "";
    String send_state = "";
    String content = "";
    String title = "";
    String emailList = "";
    String trade_no = "";
    MailMgr emailMgr = new MailMgr();
    Map temEmailMap = new Hashtable();
    List recordEmailList = new ArrayList();
    Hashtable map = null;
    if ((taskList != null) && (taskList.size() > 0))
    {
      for (int i = 0; i < taskList.size(); i++)
      {
        map = (Hashtable)taskList.get(i);
        trade_no = this.randomID.GenOrderId();
        if (map.get("email") != null) email = map.get("email").toString();
        if (map.get("send_state") != null) send_state = map.get("send_state").toString();
        if (map.get("content") != null) content = map.get("content").toString();
        if (map.get("title") != null) title = map.get("title").toString();

        emailMgr.sendMail(email, title, content);

        map.remove("send_state");
        map.put("send_state", "0");
        map.put("new_trade_id", trade_no);
        recordEmailList.add(map);
      }

      this.emailRecordInfo.recordEmails(recordEmailList);
    }
  }

  private String getCurrentDate()
  {
    Date currentDate = new Date();

    DateFormat formater = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    String currentTime = formater.format(currentDate);

    return currentTime;
  }

  public static void main(String[] args)
  {
    TaskScheduler taskScheduler = new TaskScheduler();

    taskScheduler.runTaskScheduler();
  }
}